package com.yishu.servlet;

import com.yishu.Dao.MyBookDao;
import com.yishu.util.DBConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

public class AddShopServlet extends HttpServlet {

    DBConnection dbConnection = new DBConnection();
    MyBookDao myBookDao = new MyBookDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userId = Integer.parseInt(req.getParameter("userId"));
        int bookId = Integer.parseInt(req.getParameter("bookId"));
        System.out.println(userId);
        System.out.println(bookId);

        try {
            Connection connection = dbConnection.getCon();
            int result = myBookDao.addShopARecourd(bookId,userId);
            dbConnection.closeCon(connection);
            if(result == 1) {
                req.setAttribute("result","添加购物车成功!");
                req.getRequestDispatcher("/addShopResult.jsp").forward(req,resp);
            }else {
                req.setAttribute("result","添加购物车失败!");
                req.getRequestDispatcher("/addShopResult.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
